{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# CS206: Lab 2\n", "\n", "

Bryn Mawr College, CS206: Data Structure
Douglas Blank, Spring 2016

\n", "\n", "[Your name in next cell as a level-2 header. Delete this line when done.]" ] }, { "cell_type": "markdown", "metadata": { "nbgrader": { "grade": true, "grade_id": "by-line", "locked": false, "points": 5, "solution": true } }, "source": [ "## by STUDENT NAME" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Goals**:\n", "\n", "1. Review of Java language\n", "1. Review of Java Classes\n", "1. First Data Structre: `LinkedList`\n", "\n", "**Rubric**:\n", "\n", "1. Not only must you have the correct answer, but it must also be formatted with proper indentation.\n", "1. Points are assigned to each test" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 1\n", "\n", "Write a Java function `letterGrade` that takes a double `score` and returns the letter grade based on the following scale:\n", "\n", "* score > 97, returns \"A+\"\n", "* score > 94, returns \"A\"\n", "* score > 90, returns \"A-\"\n", "* score > 87, returns \"B+\"\n", "* score > 84, returns \"B\"\n", "* score > 80, returns \"B-\"\n", "* score > 77, returns \"C+\"\n", "* score > 74, returns \"C\"\n", "* score > 70, returns \"C-\"\n", "* score > 67, returns \"D+\"\n", "* score > 64, returns \"D\"\n", "* score > 60, returns \"D-\"\n", "* otherwise, returns \"F\"\n", "\n", "Put your function definition by itself in the following cell:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "a2-problem1", "locked": false, "points": 10, "solution": true } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Added method letterGrade(double)\n", "\n" ] } ], "source": [ "String letterGrade(double score) {\n", " if (score > 97) \n", " return \"A+\";\n", " else if (score > 94)\n", " return \"A\";\n", " else if (score > 90)\n", " return \"A-\";\n", " else if (score > 87)\n", " return \"B+\";\n", " else if (score > 84)\n", " return \"B\";\n", " else if (score > 80)\n", " return \"B-\";\n", " else if (score > 77)\n", " return \"C+\";\n", " else if (score > 74)\n", " return \"C\";\n", " else if (score > 70)\n", " return \"C-\";\n", " else if (score > 67)\n", " return \"D+\";\n", " else if (score > 64)\n", " return \"D\";\n", " else if (score > 60)\n", " return \"D-\";\n", " else \n", " return \"F\";\n", "}" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "a2-problem1-1", "locked": true, "points": 5, "solution": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Expression value is: \"A+\"\n", "| assigned to temporary variable $10 of type String\n", "\n" ] }, { "data": { "text/plain": [ "A+" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letterGrade(100);" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "a2-problem1-2", "locked": true, "points": 5, "solution": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Expression value is: \"A\"\n", "| assigned to temporary variable $11 of type String\n", "\n" ] }, { "data": { "text/plain": [ "A" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letterGrade(95);" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "a2-problem1-3", "locked": true, "points": 5, "solution": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Expression value is: \"B+\"\n", "| assigned to temporary variable $12 of type String\n", "\n" ] }, { "data": { "text/plain": [ "B+" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letterGrade(89.9);" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "a2-problem1-4", "locked": true, "points": 5, "solution": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Expression value is: \"F\"\n", "| assigned to temporary variable $13 of type String\n", "\n" ] }, { "data": { "text/plain": [ "F" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letterGrade(59.99);" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "a2-problem1-5", "locked": true, "points": 5, "solution": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Expression value is: \"D\"\n", "| assigned to temporary variable $3 of type String\n", "\n" ] }, { "data": { "text/plain": [ "D" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letterGrade(65);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 2\n", "\n", "Write a Java class `Robot` that takes an double x and y in the constructor. The robot needs to keep track of where it is in the world.\n", "\n", "Methods:\n", "\n", "* moveUp(double distance)\n", "* moveDown(double distance)\n", "* moveLeft(double distance)\n", "* moveRight(double distance)\n", "\n", "Make it so that the robot can't move beyond (0,0) and (10,10). If it tries, it will be right at the boundary value. For example, if it were at (1, 5) and tried to move left 2, then it would be at (0, 5).\n", "\n", "Define your class by itself in the following cell:" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "a2-problem2", "locked": false, "points": 10, "solution": true } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Modified class Robot\n", "| Update overwrote class Robot\n", "\n" ] } ], "source": [ "class Robot {\n", " double x;\n", " double y;\n", " Robot(double x, double y) {\n", " this.x = x;\n", " this.y = y;\n", " }\n", "\n", " void move(double dx, double dy) {\n", " this.x += dx;\n", " this.y += dy;\n", " \n", " this.x = Math.min(Math.max(this.x, 0), 10);\n", " this.y = Math.min(Math.max(this.y, 0), 10);\n", " }\n", "\n", " void moveUp(double distance) {\n", " move(0, -distance);\n", " }\n", " void moveDown(double distance) {\n", " move(0, distance); \n", " }\n", " void moveLeft(double distance) {\n", " move(-distance, 0);\n", " }\n", " void moveRight(double distance) {\n", " move(distance, 0);\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "a2-problem2-1", "locked": true, "points": 5, "solution": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Modified variable robot of type Robot with initial value Robot@13b6d03\n", "\n", "\n", "\n", "| Expression value is: 2.0\n", "| assigned to temporary variable $28 of type double\n", "\n" ] }, { "data": { "text/plain": [ "2.0" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Robot robot = new Robot(0, 0);\n", "robot.moveDown(1);\n", "robot.moveDown(1);\n", "robot.y" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "a2-problem2-2", "locked": true, "points": 5, "solution": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Modified variable robot of type Robot with initial value Robot@3ecf72fd\n", "\n", "\n", "\n", "\n", "| Expression value is: 6.0\n", "| assigned to temporary variable $33 of type double\n", "\n" ] }, { "data": { "text/plain": [ "6.0" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Robot robot = new Robot(0, 0);\n", "robot.moveRight(5);\n", "robot.moveRight(6);\n", "robot.moveLeft(4);\n", "robot.x" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "a2-problem2-3", "locked": true, "points": 5, "solution": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Modified variable robot of type Robot with initial value Robot@5474c6c\n", "\n", "\n", "\n", "\n", "| Expression value is: 1.2\n", "| assigned to temporary variable $42 of type double\n", "\n" ] }, { "data": { "text/plain": [ "1.2" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Robot robot = new Robot(0, 0);\n", "robot.moveDown(2.5);\n", "robot.moveUp(5.75);\n", "robot.moveDown(1.2);\n", "robot.y" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "a2-problem2-4", "locked": true, "points": 5, "solution": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Modified variable robot of type Robot with initial value Robot@61443d8f\n", "\n", "\n", "\n", "| Expression value is: 10.0\n", "| assigned to temporary variable $46 of type double\n", "\n" ] }, { "data": { "text/plain": [ "10.0" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Robot robot = new Robot(0, 0);\n", "robot.moveRight(5);\n", "robot.moveDown(5);\n", "robot.y + robot.x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 3\n", "\n", "Write Java class `LinkedList` and `Node`.\n", "\n", "Node:\n", "\n", "* constructor takes an integer (called `value`)\n", "* class has a property called `next` that is of type `Node`\n", "* a method called `append` that takes an node and either:\n", " * if next is null, puts it there\n", " * else, tells next to append it\n", "\n", "LinkedList:\n", "\n", "* has a propery called `list` that is of type `Node`\n", "* has a method called `length()` that returns how many nodes are in list\n", "* has a method called `append()` that takes a node and adds it to the end of the list\n", "\n", "Define your class `Node` by itself in the following cell:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "a2-problem3-node", "locked": false, "points": 10, "solution": true } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Added class Node\n", "\n" ] } ], "source": [ "class Node {\n", " int value;\n", " Node next;\n", " \n", " Node(int value) {\n", " this.value = value;\n", " }\n", " \n", " void append(Node node) {\n", " if (next == null) {\n", " next = node;\n", " } else {\n", " next.append(node);\n", " }\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "a2-problem3-node-1", "locked": true, "points": 5, "solution": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Added variable node of type Node with initial value Node@6956de9\n", "\n", "| Expression value is: 42\n", "| assigned to temporary variable $6 of type int\n", "\n" ] }, { "data": { "text/plain": [ "42" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Node node = new Node(42);\n", "node.value" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "a2-problem3-node-2", "locked": true, "points": 5, "solution": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Modified variable node of type Node with initial value Node@12bc6874\n", "\n", "| Expression value is: null\n", "| assigned to temporary variable $8 of type Node\n", "\n" ] }, { "data": { "text/plain": [ "null" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Node node = new Node(-1);\n", "node.next" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "a2-problem3-node-3", "locked": true, "points": 5, "solution": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Modified variable node of type Node with initial value Node@2471cca7\n", "\n", "\n", "| Expression value is: 1\n", "| assigned to temporary variable $12 of type int\n", "\n" ] }, { "data": { "text/plain": [ "1" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Node node = new Node(0);\n", "node.append(new Node(1));\n", "node.next.value" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "a2-problem3-node-4", "locked": true, "points": 5, "solution": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Modified variable node of type Node with initial value Node@763d9750\n", "\n", "\n", "\n", "| Expression value is: 2\n", "| assigned to temporary variable $16 of type int\n", "\n" ] }, { "data": { "text/plain": [ "2" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Node node = new Node(0);\n", "node.append(new Node(1));\n", "node.append(new Node(2));\n", "node.next.next.value" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define your class `LinkedList` in the following cell:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "a2-problem3-ll", "locked": false, "points": 10, "solution": true } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Added class LinkedList\n", "\n" ] } ], "source": [ "class LinkedList {\n", " Node list;\n", " \n", " int length() {\n", " Node current = list;\n", " int count = 0;\n", " while (current != null) {\n", " current = current.next;\n", " count++;\n", " }\n", " return count;\n", " }\n", " \n", " void append(Node node) {\n", " if (list == null) {\n", " list = node;\n", " } else {\n", " list.append(node);\n", " }\n", " }\n", "}\n", "\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "a2-problem3-ll-1", "locked": true, "points": 10, "solution": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Added variable ll of type LinkedList with initial value LinkedList@4ca8195f\n", "\n", "| Expression value is: 0\n", "| assigned to temporary variable $4 of type int\n", "\n" ] }, { "data": { "text/plain": [ "0" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "LinkedList ll = new LinkedList();\n", "ll.length()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "a2-problem3-ll-2", "locked": true, "points": 10, "solution": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Modified variable ll of type LinkedList with initial value LinkedList@12bc6874\n", "\n", "\n", "| Expression value is: 1\n", "| assigned to temporary variable $8 of type int\n", "\n" ] }, { "data": { "text/plain": [ "1" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "LinkedList ll = new LinkedList();\n", "ll.append(new Node(100))\n", "ll.length()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "a2-problem3-ll-3", "locked": true, "points": 10, "solution": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Modified variable ll of type LinkedList with initial value LinkedList@1ef7fe8e\n", "\n", "\n", "\n", "| Expression value is: 2\n", "| assigned to temporary variable $12 of type int\n", "\n" ] }, { "data": { "text/plain": [ "2" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "LinkedList ll = new LinkedList();\n", "ll.append(new Node(100))\n", "ll.append(new Node(67))\n", "ll.length()" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "a2-problem3-ll-4", "locked": true, "points": 10, "solution": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Modified variable ll of type LinkedList with initial value LinkedList@6979e8cb\n", "\n", "\n", "\n", "\n", "| Expression value is: 100\n", "| assigned to temporary variable $17 of type int\n", "\n" ] }, { "data": { "text/plain": [ "100" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "LinkedList ll = new LinkedList();\n", "\n", "for (int i = 0; i < 100; i++) {\n", " ll.append(new Node(i));\n", "}\n", "\n", "ll.length()" ] } ], "metadata": { "kernelspec": { "display_name": "Java 9", "language": "java", "name": "java9" }, "language_info": { "file_extension": ".class", "mimetype": "application/java-vm", "name": "java" } }, "nbformat": 4, "nbformat_minor": 0 }